package com.star.oauth2.client.controller;

import com.star.oauth2.client.model.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 消息控制器
 * 
 * 代理对资源服务器消息API的调用
 * 
 * @author star
 */
@Slf4j
@RestController
@RequestMapping("/api/client/messages")
public class MessageController {

    @Value("${resource-server.url}")
    private String resourceServerUrl;

    private final RestTemplate restTemplate = new RestTemplate();

    /**
     * 获取消息列表
     */
    @GetMapping
    public ResponseEntity<List<Message>> getMessages(@RequestHeader("Authorization") String authHeader) {
        String url = resourceServerUrl + "/api/messages";
        
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", authHeader);
        
        HttpEntity<String> entity = new HttpEntity<>(headers);
        
        log.info("调用资源服务器获取消息列表: {}", url);
        return restTemplate.exchange(
                url,
                HttpMethod.GET,
                entity,
                new ParameterizedTypeReference<List<Message>>() {}
        );
    }

    /**
     * 获取单个消息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Message> getMessage(
            @PathVariable Long id,
            @RequestHeader("Authorization") String authHeader) {
        
        String url = resourceServerUrl + "/api/messages/" + id;
        
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", authHeader);
        
        HttpEntity<String> entity = new HttpEntity<>(headers);
        
        log.info("调用资源服务器获取消息: {}", url);
        return restTemplate.exchange(
                url,
                HttpMethod.GET,
                entity,
                Message.class
        );
    }

    /**
     * 创建新消息
     */
    @PostMapping
    public ResponseEntity<Message> createMessage(
            @RequestBody Message message,
            @RequestHeader("Authorization") String authHeader) {
        
        String url = resourceServerUrl + "/api/messages";
        
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", authHeader);
        
        HttpEntity<Message> entity = new HttpEntity<>(message, headers);
        
        log.info("调用资源服务器创建消息: {}", url);
        return restTemplate.exchange(
                url,
                HttpMethod.POST,
                entity,
                Message.class
        );
    }

    /**
     * 更新消息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Message> updateMessage(
            @PathVariable Long id,
            @RequestBody Message message,
            @RequestHeader("Authorization") String authHeader) {
        
        String url = resourceServerUrl + "/api/messages/" + id;
        
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", authHeader);
        
        HttpEntity<Message> entity = new HttpEntity<>(message, headers);
        
        log.info("调用资源服务器更新消息: {}", url);
        return restTemplate.exchange(
                url,
                HttpMethod.PUT,
                entity,
                Message.class
        );
    }

    /**
     * 删除消息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteMessage(
            @PathVariable Long id,
            @RequestHeader("Authorization") String authHeader) {
        
        String url = resourceServerUrl + "/api/messages/" + id;
        
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", authHeader);
        
        HttpEntity<String> entity = new HttpEntity<>(headers);
        
        log.info("调用资源服务器删除消息: {}", url);
        return restTemplate.exchange(
                url,
                HttpMethod.DELETE,
                entity,
                Void.class
        );
    }
}
